<template>
  <div>

    <el-row :gutter="24">
      <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">

        <div class="title">{{ $t("run.feed") }}
          <el-button type="primary"
            :disabled="materialBtn || stepObj.isActive != 1 || phaseIsdis || isStep || isoperation == 1 || (stepObj.serialNo == 1 || stepObj.serialNo == 2 || stepObj.serialNo == 3) || unitStat != 10"
            size="small" style="float:right" @click="handleAdd">{{$t('common.add')}}</el-button>
        </div>
        <div class="tableAuto tableAutos">
          <el-table border :data="tableData" style="width: 100%" @row-click="rowClick" :header-cell-style="{background:'#EAEBEC'}"
            v-loading="tableLoading" :key="times" ref="refTable" highlight-current-row>
            <el-table-column label="" width="50" align="center">
              <template slot-scope="scope">
                <div style="height:23px">
                  <el-radio :label="scope.row.key" v-model="radioId" v-if="scope.row.fid">&nbsp;</el-radio>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="seq" :label="$t('common.number')" width=" 50" align="center"></el-table-column>
            <el-table-column prop="item" :label="$t('basic.material')" :width="istable2 ? '150px' : ''" show-overflow-tooltip
              align="center"></el-table-column>
            <el-table-column prop="qtyPre" :label="$t('run.planned_advertising_volume')" :width="istable2 ? '' : '100px'"
              align="center"></el-table-column>
            <el-table-column prop="actualQty" :label="$t('run.actual_advertising_volume')" align="center" v-if="istable2"></el-table-column>
            <el-table-column prop="hi" :label="$t('common.upper_limit')" align="center" v-if="istable2"></el-table-column>
            <el-table-column prop="lo" :label="$t('common.lower_limit')" align="center" v-if="istable2"></el-table-column>
            <!-- <el-table-column prop="lot" label="批次" align="center"></el-table-column> -->
            <el-table-column prop="uom" :label="$t('basic.company')" width="50px" align="center"></el-table-column>
          </el-table>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
        <div class="title">{{ $t("run.output") }}
          <el-button type="primary" size="small"
            :disabled="produceBtn || phaseIsdis || isStep || stepObj.isActive != 1 || isoperation == 1 || unitStat != 10"
            :loading="ProduceIng" style="float:right" @click="handleAddRight">{{$t('common.add')}}</el-button>
        </div>
        <div class="tableAuto tableAutos">
          <el-table border :data="produceList" style="width: 100%" @row-click="rowClickRight" :header-cell-style="{background:'#EAEBEC'}"
            v-loading="tableLoading" :key="times" ref="singleTable" highlight-current-row>
            <el-table-column label="" width="40" align="center">
              <template slot-scope="scope">
                <div style="height:23px">
                  <el-radio :label="scope.row.seq" v-model="radioIdRight" v-if="scope.row.fid">&nbsp;</el-radio>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="seq" width="50" :label="$t('common.number')" align=" center"></el-table-column>
            <el-table-column prop="item" :label="$t('basic.material')" :width="istable1 ? '190px' : ''" show-overflow-tooltip
              align="center"></el-table-column>
            <el-table-column prop="qty" :label="$t('run.plan_out_num')" :width="istable1 ? '' : '100px'" align="center"></el-table-column>
            <el-table-column prop="actualQty" :label="$t('run.real_out_num')" align="center" v-if="istable1"></el-table-column>
            <el-table-column prop="hi" :label="$t('common.upper_limit')" align="center" v-if="istable1"></el-table-column>
            <el-table-column prop="lo" :label="$t('common.lower_limit')" align="center" v-if="istable1"></el-table-column>
            <!-- <el-table-column prop="lot" label="批次" align="center"></el-table-column> -->
            <el-table-column prop="uom" :label="$t('basic.company')" width="50px" align="center"></el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>

    <el-dialog v-dialogDrag :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"
      :close-on-click-modal="false">
      <el-form :model="ruleForm" label-width="85px" ref="ruleForm" :rules="rules" size="small" :hide-required-asterisk="true">
        <el-row :gutter="24">
          <el-col :xs="24">
            <el-form-item label="物料名称" class="ItemCode" prop="item">
              <el-input v-model="ruleForm.item" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="投放量" prop="qtyPre">
              <el-input v-model="ruleForm.qtyPre" placeholder="" v-enterNumber2 type="number" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="投放量" prop="actualQty">
              <el-input v-model="ruleForm.actualQty" placeholder="" type="number"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="批次" prop="lot">
              <el-input v-model="ruleForm.lot" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.upper_limit')" prop="hi">
              <el-input v-model="ruleForm.hi" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.lower_limit')" prop="lo">
              <el-input v-model="ruleForm.lo" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('basic.company')">
              <el-input v-model="ruleForm.uom" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="loading" :disabled="stepObj.isActive != 1" type="primary" size="small" @click="handleConfirm('ruleForm')">确
          认</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>

    <el-dialog v-dialogDrag :title="title1" :visible.sync="dialogVisible1" width="30%" :before-close="handleCloseRight"
      :close-on-click-modal="false">
      <el-form :model="formData" label-width="85px" ref="produceForm" :rules="produceRules" size="small" :hide-required-asterisk="true">
        <el-row :gutter="24">
          <el-col :xs="24">
            <el-form-item label="物料名称" class="ItemCode" prop="item">
              <el-select v-model="formData.item" remote filterable :remote-method="remoteMethodItemCode" placeholder=""
                v-el-select-loadmore="loadMoreItemCodeFun" clearable @clear="clearItemCode" @change="changeCodeAlert" disabled>
                <el-option v-for="(item,index) in itemCodeList" :key="index" :label="item.fid" :value="item.fid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="数量" prop="qty">
              <el-input v-enterNumber2 type="number" disabled v-model="formData.qty" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="产出量" prop="actualQty">
              <el-input v-model="formData.actualQty" placeholder="" type="number"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="批次" prop="lot">
              <el-input v-model="formData.lot" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.upper_limit')" prop="hi">
              <el-input v-model="formData.hi" disabled placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('common.lower_limit')" prop="lo">
              <el-input v-model="formData.lo" disabled placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('basic.company')">
              <el-input v-model="formData.uom" placeholder="" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleProduceConfirm('produceForm')">{{ $t("run.confirm") }}</el-button>
        <el-button @click="handleCloseRight()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>

    <el-dialog v-dialogDrag title="签名" :visible.sync="dialogVisible2" width="40%" :before-close="handleCloseSignName"
      :close-on-click-modal="false">
      <el-form :model="loginForm" label-width="85px" ref="loginForm" :rules="loginRules" size="small" :hide-required-asterisk="true">
        <el-row :gutter="24">
          <el-col :xs="24">
            <el-form-item label="用户名" prop="userName">
              <el-input type="text" v-model="loginForm.userName" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item label="密码" prop="passWord" class="password">
              <el-input type="text" v-model="loginForm.passWord" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleConfirmLogin('loginForm')">{{ $t("run.confirm") }}</el-button>
        <el-button @click="handleCloseSignName()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
    <div class="btnfang" style="text-align:center;margin-top:18px">
      <!-- phaseCode  TEM  温度控制暂时把暂停 启动 退出 禁止掉 -->
      <el-button :disabled="(stepObj.isActive != 1 || button1 || phaseIsdis) || isStep || isoperation == 1" type="primary" size="small"
        @click="handleEditPhaseStatus(10,'启动')">{{ $t("run.start_up") }}</el-button>
      <el-button :disabled="stepObj.isActive != 1 || button2 || phaseIsdis || isStep || isoperation == 1 || stepObj.phaseCode == 'PH_TEM'"
        size="small" @click="handleEditPhaseStatus(41,'停止')">{{ $t("run.stop") }}</el-button>
    </div>
  </div>
</template>

<script>
import { configure } from "@/api/configure"
import { formula } from "@/api/formula"
import { scheduling } from "@/api/scheduling"
import { plc } from "@/api/plc"
export default {
  data () {
    let HiFn = (rule, value, callback) => {
      if (value) {
        if (parseFloat(this.ruleForm.qtyPre) > parseInt(value)) {
          callback(new Error('物料上限不能小于物料数量'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    }
    let LoFn = (rule, value, callback) => {
      if (value) {
        if (parseFloat(value) > parseFloat(this.ruleForm.qtyPre)) {
          callback(new Error('物料下限不能大于物料数量'));
        } else if (this.ruleForm.hi) {
          if (parseFloat(value) > parseFloat(this.ruleForm.hi)) {
            callback(new Error('物料下限不能大于物料上限'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      } else {
        callback();
      }
    }
    let produceHiFn = (rule, value, callback) => {
      if (value) {
        if (parseFloat(this.formData.qty) > parseInt(value)) {
          callback(new Error('产出上限不能小于物料数量'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    }
    let produceLoFn = (rule, value, callback) => {
      if (value) {
        if (parseFloat(value) > parseFloat(this.formData.qty)) {
          callback(new Error('产出下限不能大于物料数量'));
        } else if (this.ruleForm.hi) {
          if (parseFloat(value) > parseFloat(this.formData.hi)) {
            callback(new Error('物料下限不能大于物料上限'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      } else {
        callback();
      }
    }
    let lotFn = (rule, value, callback) => {
      if (!value) {
        callback(new Error('批次号不能为空'));
      } else {
        callback();
      }
      // if(parseInt(this.stepObj.serialNo) === 0 || this.stepObj.serialNo == 8){
      //     if(!value){
      //         callback(new Error('批次号不能为空'));
      //     }else{
      //         callback();
      //     }
      // }else{
      //     if(value){
      //         scheduling.checkLot({lotNum:value}).then(res => {
      //             if(res.code == 200){
      //                 callback();
      //             }else{
      //                 callback(new Error('批次号已存在'));
      //             }
      //         })
      //     }else{
      //         callback(new Error('批次号不能为空'));
      //     }
      // }

    }
    return {
      tableData: ['', '', ''],
      radioId: "",
      itemCodeList: [],
      keywordClassId: "",
      pagerClass: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      formData: {
        item: "",
        qty: "",
        lot: "",
        uom: "",
        flastModifyComment: "",
        actualQty: "",
        hi: "",
        lo: "",
        seq: 0
      },
      ruleForm: {
        item: "",
        qtyPre: "",
        actualQty: "",
        lot: "",
        uom: "",
        hi: "",
        lo: ""
      },
      rules: {
        item: [{ required: true, message: '物料名称不能为空', trigger: 'change' }],
        qtyPre: [{ required: true, message: '投放量不能为空', trigger: 'blur' }],
        actualQty: [{ required: true, message: '投放量不能为空', trigger: 'blur' }],
        lot: [{ validator: lotFn, trigger: 'blur' }],
        hi: [{ validator: HiFn, trigger: 'blur' }],
        lo: [{ validator: LoFn, trigger: 'blur' }]
      },
      dialogVisible: false,
      tableLoading: false,
      title: "添加",
      currObj: {},
      loading: false,
      ProduceIng: false,
      disabled: true,
      dialogVisible1: false,
      dialogVisible2: false,
      title1: "添加",
      produceRules: {
        item: [{ required: true, message: '物料名称不能为空', trigger: 'change' }],
        qty: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        lot: [{ validator: lotFn, trigger: 'blur' }],
        actualQty: [{ required: true, message: '产出量不能为空', trigger: 'blur' }],
        hi: [{ validator: produceHiFn, trigger: 'blur' }],
        lo: [{ validator: produceLoFn, trigger: 'blur' }]
      },
      produceList: ['', '', ''],
      radioIdRight: "",
      loginForm: {
        userName: "",
        passWord: ""
      },
      loginRules: {
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
      materialBtn: true,
      produceBtn: true,
      arrLength: 0,
      times: 0,
      doneByUserId: "",
    }
  },
  props: {
    stepObj: Object,
    count: Number,
    woId: String,
    phaseIsdis: Boolean,
    formsSeqNo: [String, Number],
    idObj: Object,
    isoperation: [Number, Object],
    unitStat: Number,
  },
  watch: {
    count (val) {
      if (val) {
        this.radioId = ''
        this.radioIdRight = ''
        let phase = ['1', '2', '3', '11', '12']  //自动加料    转料的id
        if (parseInt(this.stepObj.serialNo) === 0) {
          this.findHisChar()
          this.materialBtn = false
        } else if (phase.indexOf(this.stepObj.serialNo) > -1) {
          this.findHisAuto()
          if (this.stepObj.serialNo == 11 || this.stepObj.serialNo == 12) {
            this.materialBtn = true
            this.produceBtn = true
          } else {
            this.materialBtn = false
            this.produceBtn = true
          }

        } else if (this.stepObj.serialNo == 8) { //物料产出
          this.findHisOtm()
          this.produceBtn = false
        } else {
          this.produceBtn = true
          this.materialBtn = true
          this.currObj = {}
          this.tableData = ['', '', '']
          this.produceList = ['', '', '']
          for (let key in this.formData) {
            this.formData[key] = ""
          }
        }
        this.times = new Date().getTime()
      }
    }
  },
  computed: {
    isStep () {
      if (parseInt(this.stepObj.seqNo || 0) == this.formsSeqNo) {
        return false
      } else {
        return true
      }
    },
    istable1 () {
      if (this.stepObj.serialNo != 11 && this.stepObj.serialNo != 12) {
        return true
      } else {
        return false
      }
    },
    istable2 () {
      if (this.stepObj.serialNo != 1 && this.stepObj.serialNo != 2 && this.stepObj.serialNo != 3) {
        return true
      } else {
        return false
      }
    },
    isInfoPhase () {
      if (parseInt(this.stepObj.serialNo) === 0 || this.stepObj.serialNo == 7 || this.stepObj.serialNo == 8 || this.stepObj.serialNo == 4 || this.stepObj.serialNo == 6 || this.stepObj.serialNo == 9 || this.stepObj.serialNo == 10) {
        return false
      } else {
        return true
      }
    },
    isSHowstopBtn () {
      if (parseInt(this.stepObj.serialNo) === 0 || this.stepObj.serialNo == 7 || this.stepObj.serialNo == 8 || this.stepObj.serialNo == 4 || this.stepObj.serialNo == 5 || this.stepObj.serialNo == 6 || this.stepObj.serialNo == 9 || this.stepObj.serialNo == 10) {
        return true
      } else {
        return false
      }
    },
    // 6温度控制   9蒸馏-氮气置换  10 蒸馏-冷凝接收
    button1 () { //启动按钮
      let arr = ['1', '2', '3', '5', '11', '12']
      let arr1 = ['0', '7', '8', '4', '6', '9', '10']
      if (this.unitStat === 0 || this.unitStat == 11) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else if (arr1.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else if (this.unitStat == 51) {
        let arr2 = ['5', '1', '2', '3', '11', '12']
        if (arr2.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    button2 () { //停止按钮
      let arr = ['5', '1', '2', '3', '11', '12']
      if (this.unitStat == 10) {
        if (arr.indexOf(this.stepObj.serialNo) > -1) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    }

  },
  //自动加料  物料产出  转料
  created () {
    this.getItemList()
  },
  methods: {
    //获取投料列表
    findHisChar () {
      if (!this.stepObj.fid) {
        this.tableData = ['', '', '']
        this.produceList = ['', '', '']
        return
      }
      const param = {
        phaseId: this.stepObj.fid,
        woId: this.woId
      }
      this.tableLoading = true
      plc.findHisChar(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data.map((el, index) => {
            return {
              item: el.itemId,
              qtyPre: el.qty,
              key: index,
              ...el,
              // seq:index+1
            }
          })
          this.produceList = []
        } else {
          this.tableData = []
          this.produceList = []
        }
        if (this.tableData.length > this.produceList.length) {
          let length = this.tableData.length - this.produceList.length
          for (let i = 0; i < length; i++) {
            this.produceList.push('')
          }
        } else if (this.produceList.length > this.tableData.length) {
          let length = this.produceList.length - this.tableData.length
          for (let i = 0; i < length; i++) {
            this.tableData.push('')
          }
        }
      })
    },
    findHisAuto () {
      if (!this.stepObj.fid) {
        this.tableData = ['', '', '']
        this.produceList = ['', '', '']
        return
      }
      const param = {
        phaseId: this.stepObj.fid,
        woId: this.woId
      }
      this.tableLoading = true
      plc.findHisAuto(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          if (res.data.length > 1) {
            res.data = [res.data[0]]
          }
          if (this.stepObj.serialNo == 1 || this.stepObj.serialNo == 2 || this.stepObj.serialNo == 3) {
            this.tableData = res.data.map((el, index) => {
              return {
                item: el.paraName,
                qtyPre: el.target,
                key: index,
                ...el,
                // seq:index+1
              }
            })
            this.currObj = this.tableData[0]
            this.produceList = []
          } else if (this.stepObj.serialNo == 11 || this.stepObj.serialNo == 12) {
            this.produceList = res.data
            this.produceList.forEach((item, index) => {
              item.key = index
              item.item = item.paraName
              item.qty = item.target
              item.seq = index + 1
            })
            this.currObj = this.produceList[0]
            this.tableData = []
          }
        } else {
          this.tableData = []
          this.produceList = []
        }
        if (this.tableData.length > this.produceList.length) {
          let length = this.tableData.length - this.produceList.length
          for (let i = 0; i < length; i++) {
            this.produceList.push('')
          }
        } else if (this.produceList.length > this.tableData.length) {
          let length = this.produceList.length - this.tableData.length
          for (let i = 0; i < length; i++) {
            this.tableData.push('')
          }
        }
      })
    },
    //获取产出信息
    findHisOtm () {
      if (!this.stepObj.fid) {
        this.tableData = ['', '', '']
        this.produceList = ['', '', '']
        return
      }
      const param = {
        phaseId: this.stepObj.fid,
        woId: this.woId
      }
      this.tableLoading = true
      plc.findHisOtm(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.produceList = res.data
          this.produceList.forEach((item, index) => {
            item.key = index
            item.item = item.itemId
            item.seq = index + 1
          })
          this.currObj = this.produceList[0]
          this.tableData = []
        } else {
          this.tableData = []
          this.produceList = []
        }
        if (this.tableData.length - this.produceList.length > 0) {
          let length = this.tableData.length - this.produceList.length
          for (let i = 0; i < length; i++) {
            this.produceList.push('')
          }
        } else if (this.produceList.length - this.tableData.length > 0) {
          let length = this.produceList.length - this.tableData.length
          for (let i = 0; i < length; i++) {
            this.tableData.push('')
          }
        }
      })
    },
    rowClick (row) {
      if (row) {
        this.radioId = row.key;
        this.currObj = row
        this.$refs.refTable.setCurrentRow(row);
      }

    },
    //获取物料编码列表
    getItemList () {
      const param = {
        fid: this.keywordClassId,
        page: this.pagerClass.pageNum,
        size: this.pagerClass.pageSize
      }
      configure.getItemList(param).then(res => {
        if (res.code == 200) {
          this.itemCodeList = [...this.itemCodeList, ...res.data.rows]
        }
      }).catch(e => {

      })
    },
    //下拉加载物料编码
    loadMoreItemCodeFun () {
      this.pagerClass.pageNum++
      this.getItemList()
    },
    //远程搜索物料编码
    remoteMethodItemCode (keyword) {
      this.keywordClassId = keyword.trim();
      this.pagerClass.pageNum = 1
      setTimeout(() => {
        this.itemCodeList = []
        this.getItemList()
      }, 200)
    },
    clearItemCode () {
      this.keywordClassId = ""
      this.pagerClass.pageNum = 1
      this.itemCodeList = []
      this.getItemList()
    },
    changeCode (val) {
      let item = this.itemCodeList.find(item => item.fid == val)
      this.formData.uom = item.uomId
    },
    changeCodeAlert (val) {
      let item = this.itemCodeList.find(item => item.fid == val)
      this.formData.uom = item.uomId
    },
    handleAdd () {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      if (this.radioId === '' || this.radioId === null || this.radioId === undefined) {
        this.$message.error('请选择物料');
        return
      }
      this.ruleForm = {}
      console.log(this.currObj)
      this.ruleForm.item = this.currObj.paraName || this.currObj.itemId
      this.ruleForm.qtyPre = this.currObj.target || this.currObj.qty
      this.ruleForm.uom = this.currObj.uom
      this.ruleForm.seq = this.currObj.seq
      this.ruleForm.phaseId = this.stepObj.fid
      this.ruleForm.woId = this.woId
      this.ruleForm.unitId = this.stepObj.operationId
      // this.ruleForm.unitId = this.idObj.equipmentId
      this.ruleForm.key = this.tableData.length + 1
      this.ruleForm.lo = this.currObj.lo
      this.ruleForm.hi = this.currObj.hi
      // this.$set(this.ruleForm,'lot',this.currObj.lot || '')
      try {
        this.$refs['ruleForm'].resetFields();
      } catch (e) { }
      this.title = "添加"
      this.dialogVisible = true
    },
    handleConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.type = "投料"
          if (this.stepObj.signitature == 1) {
            this.dialogVisible2 = true
            return
          }
          this.doneByUserId = ''
          this.handleMaterialSubmit()
        }
      })
    },
    handleClose () {
      this.dialogVisible = false
    },
    rowClickRight (row) {
      if (row) {
        this.radioIdRight = row.seq;
        this.$refs.singleTable.setCurrentRow(row);
      }

    },
    handleAddRight () {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      if (this.radioIdRight === '') {
        this.$message.error('请选择物料');
        return
      }
      this.formData = {}
      this.formData.item = this.currObj.paraName || this.currObj.itemId
      this.formData.uom = this.currObj.uom
      this.formData.lo = this.currObj.lo
      this.formData.hi = this.currObj.hi
      this.formData.qty = this.currObj.qty
      this.formData.qtyPre = this.currObj.target || this.currObj.qty
      this.formData.phaseId = this.stepObj.fid
      this.formData.unitId = this.stepObj.operationId
      this.formData.seq = this.currObj.seq
      this.formData.phaseId = this.stepObj.fid
      // this.$set(this.formData,'lot',this.currObj.lot || '')
      try {
        this.$refs['produceForm'].resetFields();
      } catch (e) { }
      this.title1 = "添加"
      this.dialogVisible1 = true
    },
    handleCloseRight () {
      this.dialogVisible1 = false
    },
    handleProduceConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.stepObj.serialNo == 11 || this.stepObj.serialNo == 12) {
            this.type = "投料"
          } else {
            this.type = "产出"
          }

          if (this.stepObj.signitature == 1) {
            this.dialogVisible2 = true
            return
          }
          this.doneByUserId = ''
          if (this.stepObj.serialNo == 11 || this.stepObj.serialNo == 12) {
            this.handleMaterialSubmit()
          } else {
            this.produceConfirm()
          }
        }
      })
    },
    handleCloseSignName () {
      this.dialogVisible2 = false
    },
    handleConfirmLogin (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = {
            fpsw: this.loginForm.passWord,
            faccount: this.loginForm.userName
          }
          plc.checkSysUser(param).then(res => {
            if (res.code == 200) {
              this.doneByUserId = res.data[0].fid
              if (this.type == "产出") {
                this.produceConfirm()
              } else {
                this.handleMaterialSubmit()
              }
            } else {
              this.$message.error(res.msg);
            }
          })
        }
      })
    },
    // 产出提交
    produceConfirm () {
      let param = {
        ...this.formData
      }
      // const index = this.produceList.findIndex(item => item.seq == this.formData.seq)
      const index = this.produceList.findIndex(item => {
        if (item.seqNo !== '' && item.seqNo !== null && item.seqNo !== undefined) {
          return item.seqNo == param.seqNo
        } else {
          return item.seq == param.seq
        }
      })

      param.qtyPre = this.formData.qtyPre ? this.formData.qtyPre : 0
      param.qtyAft = this.produceList[index].qtyAft ? this.produceList[index].qtyAft : 0
      param.qtyAft = parseFloat(this.produceList[index].actualQty || 0) + parseFloat(this.formData.actualQty || 0)
      param.qty = parseFloat(this.formData.actualQty || 0)
      param.lot = this.formData.lot
      // param.unitId = this.idObj.opId
      param.unitId = this.idObj.equipmentId
      param.woId = this.idObj.woId
      param.jobId = this.idObj.jobId
      param.doneByUserId = this.doneByUserId
      if (param.seqNo === '' || param.seqNo === null || param.seqNo === undefined) {
        param.seqNo = param.seq
      }
      console.log(param)
      this.ProduceIng = true
      plc.submitProduceCommand([param]).then(res => {
        this.ProduceIng = false
        if (res.code == 200) {
          this.dialogVisible2 = false
          this.dialogVisible1 = false
          this.findHisOtm()
          try {
            this.$refs["loginForm"].resetFields();
          } catch (e) { }
          this.$message({ duration: 1000, type: 'success', message: '提交成功!' });
        }
      }).catch(e => {
        this.$message.error('服务器异常,请稍后再试');
        this.ProduceIng = false
      })
    },
    // 投料提交
    handleMaterialSubmit () {
      let param = {}
      let tableData = []
      if (this.stepObj.serialNo == 11 || this.stepObj.serialNo == 12) {
        tableData = this.produceList
        param = {
          ...this.formData,
        }
      } else {
        tableData = this.tableData
        param = {
          ...this.ruleForm,
        }
      }
      console.log(tableData)
      // const index = tableData.findIndex(item => item.seq == param.seq)
      const index = tableData.findIndex(item => {
        if (item.seqNo !== '' && item.seqNo !== null && item.seqNo !== undefined) {
          return item.seqNo == param.seqNo
        } else {
          return item.seq == param.seq
        }
      })
      param.qtyPre = param.qtyPre ? param.qtyPre : 0
      param.qtyAft = tableData[index].qtyAft ? tableData[index].qtyAft : 0
      param.qtyAft = parseFloat(tableData[index].actualQty || 0) + parseFloat(param.actualQty || 0)
      param.qty = parseFloat(param.actualQty || 0)
      param.lot = param.lot
      // param.unitId = this.idObj.opId
      param.unitId = this.idObj.equipmentId
      param.woId = this.idObj.woId
      param.jobId = this.idObj.jobId
      param.doneByUserId = this.doneByUserId
      if (param.seqNo === '' || param.seqNo === null || param.seqNo === undefined) {
        param.seqNo = param.seq
      }
      console.log(param)
      this.loading = true
      plc.submitFeedingCommand([param]).then(res => {
        this.loading = false
        if (res.code == 200) {
          let phase = ['1', '2', '3', '11', '12']  //自动加料
          if (parseInt(this.stepObj.serialNo) === 0) {
            this.findHisChar()
          } else if (phase.indexOf(this.stepObj.serialNo) > -1) {
            this.findHisAuto()
          }
          try {
            this.$refs["loginForm"].resetFields();
          } catch (e) { }

          this.dialogVisible = false
          this.dialogVisible1 = false
          this.dialogVisible2 = false
          this.$message({ duration: 1000, type: 'success', message: '添加成功!' });
        }
      }).catch(e => {
        this.loading = false
        this.$message.error('服务器异常,请稍后再试');
      })
    },
    handleEditPhaseStatus (type, txt) {
      if (!this.stepObj.fid) {
        this.$message.error('请选择阶段');
        return
      }
      let that = this
      this.$customConfirm({
        content: '是否要' + txt + '此阶段',
        onok () {
          const param = {
            fid: that.stepObj.fid,
            stateCd: type
          }
          plc.sendPlcCommandPhase(param).then(res => {
            if (res.code == 200) {
              that.$message({ duration: 1000, type: 'success', message: txt + '成功!' });
              if (type == 40 && that.stepObj.serialNo == 7) {
                that.submitDescribeCommand()
              }

            }
          })
        },
      })
    },
  }
}
</script>
<style scoped>
.title {
  font-size: 18px;
  margin-bottom: 15px;
}
.groudBtn {
  text-align: right;
  margin-top: 10px;
  margin-bottom: 10px;
}
.ItemCode >>> .el-select {
  width: 100%;
}
.password >>> .el-input {
  -webkit-text-security: disc;
}
</style>